// Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
//
// Licensed under the Mulan PSL v2.
// You can use this file according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of the License at
//
//     http://license.coscl.org.cn/MulanPSL2
//
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
// FOR MORE DETAILS.

/*
 * @Author: qiulin_JXGM ql1746365080@gmail.com
 * @Date: 2024-10-11 15:36:29
 * @LastEditors: qiulin ql1746365080@gmail.com
 * @LastEditTime: 2024-12-12 14:26:05
 * @FilePath: \SGFEM\Import\BDFImport\BulkData\Constraint\MPCADDParser.cpp
 */

#include "Import/BDFImport/BulkData/Constraint/MPCADDParser.h"

#include <algorithm>
#include <iostream>

#include "Utility/Logging/SGLogger.h"

#include "DBManager/DBServiceFactory.h"
#include "DBManager/IService/Input/Constraint/IMPCADDService.h"

#include "Import/BDFImport/BDFParserBase.h"

BULKDATA_ENTRY_PARSER_REG (MPCADD);

using namespace SG::DBManager;
using namespace SG::Import;
using namespace SG::DataStructure;

/*
// file://./../../Doc/MarkDown/BulkData/MPCADD/MPCADD.md>
*/

SG::DataStructure::Common::Status BDF::BULKDATA::ParseMPCADD (const std::shared_ptr<SG::DBManager::DBServiceFactory>& dbServiceFactory,
                                                              std::vector<std::string>&                               dividedEntryInfo)
{
    SG::DataStructure::FEM::MPCADDData tempData;

    // TODO(bug 001) m_dominId的正确填写
    tempData.m_domainId = 1;

    // 提取并转换词条信息的第二个字段为 m_sId
    tempData.m_sId = convertToI64 (dividedEntryInfo[1]);

    // 计算分割后的行数，每 10 个字段为一行
    const size_t rowNum = dividedEntryInfo.size () / 10;

    // 遍历每一行的条目
    for (size_t curRow = 0; curRow < rowNum; curRow++)
    {
        // 遍历每行的第 2 到第 9 个字段（不包含首字段）
        for (size_t fieldsColumn = 1; fieldsColumn < 9; fieldsColumn++)
        {
            // 计算当前字段的索引
            const size_t curIndex  = curRow * 10 + fieldsColumn;
            std::string& curStrKey = dividedEntryInfo[curIndex];
            // 检查当前字段是否为空白
            bool const isEmpty = is_all_space (curStrKey);

            // 如果当前字段为空白，跳过处理
            if (isEmpty)
            {
                continue;
            }

            // 将非空字段转换为长整型并添加到子集数据中
            tempData.m_subSets.push_back (convertToI64 (dividedEntryInfo[curIndex]));
        }
    }
    return dbServiceFactory->get<IMPCADDService> ()->Add (tempData);
}